package service;

import common.*;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Vector;

//用以登录
public class UserClientService {
    private User user;
    private Socket socket;
    public Message ms2;
    public User getUser() {
        return user;
    }
    public Message getMs2(){
        return ms2;
    }
    public Socket getSocket() {
        return socket;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public boolean UserCheck(String id, String pwd){
        boolean re=false;
        user =new User(id,pwd);
        try {
            socket=new Socket("127.0.0.1",10086);
            ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
            Message ms1=new Message();//向服务器发送登录请求
            ms1.setState(MessageType.Login_Require);
            oos.writeObject(ms1);
            //将用户发送到服务端
            oos.writeObject(user);
            //从服务器接收消息，是否登录成功
            ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
            ms2=(Message) ois.readObject();
            if(ms2.getState()== MessageType.Longin_Success){
                System.out.println("登录成功");
                user=(User) ois.readObject();
                //登录成功，创建与服务器保持通信的线程
                ClientConnectServer ccs=new ClientConnectServer(socket,user);
                ccs.start();
                //将线程放入集合中管理
                ControlCSConnectionThread.addCSThread(user.getId(), ccs);
                re=true;
            }else{
                //无法启动线程
                //但socket是打开的
                System.out.println("登陆失败");
                socket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return re;
    }
    public boolean UserRegister(String id,String pwd,String code,String name,String phone,String birth){
        boolean re=false;
        user =new User(id,pwd,new Code(code),name,phone,birth);
        try {
            socket = new Socket("127.0.0.1", 10086);
            //向服务端发送注册申请
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            Message ms1=new Message();//向服务端发送注册请求
            ms1.setState(MessageType.Register_Require);
            oos.writeObject(ms1);
            //将用户发送到服务端
            oos.writeObject(user);
            //从服务器接收消息，是否注册成功
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            ms2 = (Message) ois.readObject();
            if(ms2.getState()==MessageType.Register_Success){
                System.out.println("注册成功");
                String ID=(String)ois.readObject();
                user.setNumber(ID);
                //注册成功，创建与服务器保持通信的线程
                ClientConnectServer ccs=new ClientConnectServer(socket,user);
                ccs.start();
                //将线程放入集合中管理
                ControlCSConnectionThread.addCSThread(user.getId(), ccs);
                re=true;
            }else{
                //无法启动线程
                //但socket是打开的
                System.out.println("注册失败");
                socket.close();
            }
        }catch (Exception e){
            System.out.println("注册申请失败");
        }
        return re;
    }
}
